home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / CRIBBAGE.PAK / BOARD.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  5KB  |  190 lines

  1. //--------------------------------------------------------------------------
  2. // Turbo Cribbage -- Copyright (c) 1995, Borland International
  3. //--------------------------------------------------------------------------
  4. #include <owl/pch.h>
  5. #include "board.h"
  6. #include <owl/gdiobjec.h>
  7.  
  8. #include "cribbage.rh"
  9.  
  10. TCribbageBoard::TCribbageBoard(TWindow* parent, int x, int y)
  11. :
  12.   TWindow(parent, "", 0)
  13. {
  14.   Attr.Style = WS_CHILD | WS_VISIBLE;
  15.   Attr.X = x;
  16.   Attr.Y = y;
  17.   Attr.W = 200;
  18.   Attr.H = 400;
  19.   points[0] = points[1] = 0;
  20.   oldpoints[0] = oldpoints[1] = -1;
  21.   playerName[0][0] = 0;
  22.   playerName[1][0] = 0;
  23.  
  24.   // initialize the array of hole positions
  25.  
  26.   int i,j;
  27.   for (i=0;i<2;i++)
  28.     for (j=0;j<123;j++)
  29.       holes[i][j] = TPoint(0,0);
  30.  
  31.   // starting holes
  32.   for (i=0;i<2;i++) {
  33.     holes[0][i] = TPoint(25, 289-i*9);
  34.     holes[1][i] = TPoint(36, 289-i*9);
  35.   }
  36.   // all straight columns
  37.   for (i=0;i<5;i++)
  38.     for (j=0;j<5;j++) {
  39.       holes[0][j*5+i+2] = TPoint(25, 270-j*46-i*9);
  40.       holes[1][j*5+i+2] = TPoint(36, 270-j*46-i*9);
  41.  
  42.       holes[1][1+55-j*5-i] = TPoint(69, 270-j*46-i*9);
  43.       holes[0][1+55-j*5-i] = TPoint(80, 270-j*46-i*9);
  44.  
  45.       holes[0][j*5+i+2+60] = TPoint(113, 270-j*46-i*9);
  46.       holes[1][j*5+i+2+60] = TPoint(124, 270-j*46-i*9);
  47.  
  48.       holes[1][1+115-j*5-i] = TPoint(157, 270-j*46-i*9);
  49.       holes[0][1+115-j*5-i] = TPoint(168, 270-j*46-i*9);
  50.     }
  51.   // last 5 holes
  52.   for (i=0;i<5;i++) {
  53.     holes[1][1+116+i] = TPoint(157, 280+i*9);
  54.     holes[0][1+116+i] = TPoint(168, 280+i*9);
  55.   }
  56.   // last hole
  57.   holes[1][122] = TPoint(157, 280+5*9+1);
  58.   holes[0][122] = TPoint(168, 280+5*9+1);
  59.   // first curve
  60.   holes[0][26+1] = TPoint(25,40);
  61.   holes[1][26+1] = TPoint(36,40);
  62.   holes[0][27+1] = TPoint(32,23);
  63.   holes[1][27+1] = TPoint(41,29);
  64.   holes[0][28+1] = TPoint(52,13);
  65.   holes[1][28+1] = TPoint(52,24);
  66.   holes[0][29+1] = TPoint(72,23);
  67.   holes[1][29+1] = TPoint(63,29);
  68.   holes[0][30+1] = TPoint(80,40);
  69.   holes[1][30+1] = TPoint(69,40);
  70.   // third curve
  71.   for (i=0;i<5;i++) {
  72.     holes[0][86+1+i] = holes[0][26+1+i]+TPoint(113-25,0);
  73.     holes[1][86+1+i] = holes[1][26+1+i]+TPoint(113-25,0);
  74.   }
  75.   holes[1][56+1] = TPoint(69, 280);
  76.   holes[0][56+1] = TPoint(80, 280);
  77.   holes[1][57+1] = TPoint(76, 297);
  78.   holes[0][57+1] = TPoint(85, 291);
  79.   holes[1][58+1] = TPoint(96, 307);
  80.   holes[0][58+1] = TPoint(96, 296);
  81.   holes[1][59+1] = TPoint(116, 297);
  82.   holes[0][59+1] = TPoint(107, 291);
  83.   holes[1][60+1] = TPoint(124, 280);
  84.   holes[0][60+1] = TPoint(113, 280);
  85. }
  86.  
  87. void TCribbageBoard::Reset() {
  88.   points[0] = points[1] = 0;
  89.   oldpoints[0] = oldpoints[1] = -1;
  90.   Invalidate();
  91. }
  92.  
  93. void TCribbageBoard::PegHole(TDC& dc, int player, int hole, PegType pegType) {
  94.   TRect r1,r2;
  95.   TBrush redBrush(TColor(255,0,0));
  96.   TBrush blueBrush(TColor(0,0,255));
  97.   TBrush brownBrush(TColor(128,128,0));
  98.  
  99.   hole++;
  100.   r1 = TRect(holes[player][hole]+TPoint(1,2), holes[player][hole]+TPoint(6,5));
  101.   r2 = TRect(holes[player][hole]+TPoint(2,1), holes[player][hole]+TPoint(5,6));
  102.  
  103.   switch (pegType) {
  104.     case ptPlayer0:
  105.       dc.FillRect(r1, redBrush);
  106.       dc.FillRect(r2, redBrush);
  107.       break;
  108.     case ptPlayer1:
  109.       dc.FillRect(r1, blueBrush);
  110.       dc.FillRect(r2, blueBrush);
  111.       break;
  112.     case ptEmpty: {
  113.       TBitmap bitmap(_hInstance, EMPTY_HOLE);
  114.       TMemoryDC memDC(dc);
  115.       memDC.SelectObject(bitmap);
  116.       dc.BitBlt(holes[player][hole].x, holes[player][hole].y,
  117.                  7, 7, memDC, 0, 0, SRCCOPY);
  118.       }
  119.       break;
  120.   }
  121. }
  122.  
  123. BOOL TCribbageBoard::AddToScore(int player, int amount) {
  124.  
  125.   if (amount==0)
  126.     return false;
  127.  
  128.   if (points[player]==121)
  129.     return true;
  130.  
  131.   TClientDC dc(HWindow);
  132.   char temp[10];
  133.  
  134.   PegHole(dc, player, oldpoints[player], ptEmpty);
  135.   oldpoints[player]=points[player];
  136.   points[player]+=amount;
  137.   if (points[player]>121) points[player]=121;
  138.   PegHole(dc, player, points[player], player);
  139.   wsprintf(temp, "%d", points[player]);
  140.   dc.SetBkColor(TColor(128, 128, 0));
  141.   dc.TextOut(150, 350+20*player, temp);
  142.  
  143.   return (points[player]==121);
  144. }
  145.  
  146. void TCribbageBoard::SetPlayerName(int player, const char *name) {
  147.   assert((player==0) || (player==1));
  148.   strncpy(playerName[player], name, 10);
  149.   playerName[player][10]=0;
  150.  
  151.   // if the player name is changed after the board is displayed,
  152.   // we have to repaint the name
  153.   //
  154.   if (HWindow) {
  155.     TClientDC dc(HWindow);
  156.     dc.SetTextColor(TColor(0, 0, 0));
  157.     dc.SetBkColor(TColor(128, 128, 0));
  158.     dc.TextOut(25, 350+player*20, playerName[player]);
  159.   }
  160. }
  161.  
  162. const char *TCribbageBoard::GetPlayerName(int player) {
  163.   assert((player==0) || (player==1));
  164.   return playerName[player];
  165. }
  166.  
  167. void TCribbageBoard::Paint(TDC& dc, bool, TRect&) {
  168.   char temp[10];
  169.   TBitmap bitmap(_hInstance, PEGBOARD);
  170.   TMemoryDC memDC(dc);
  171.   memDC.SelectObject(bitmap);
  172.   dc.BitBlt(0, 0, 200, 400, memDC, 0, 0, SRCCOPY);
  173.   dc.SetTextColor(TColor(0, 0, 0));
  174.   dc.SetBkColor(TColor(128, 128, 0));
  175.   dc.TextOut(25, 350, playerName[0]);
  176.   dc.TextOut(25, 370, playerName[1]);
  177.   wsprintf(temp,"%d",points[0]);
  178.   dc.TextOut(150, 350, temp);
  179.   wsprintf(temp,"%d",points[1]);
  180.   dc.TextOut(150, 370, temp);
  181.  
  182.   for (int i=0;i<2;i++) {
  183.     PegHole(dc, i, 0, ptEmpty);
  184.     PegHole(dc, i, -1, ptEmpty);
  185.     PegHole(dc, i, points[i], i);
  186.     PegHole(dc, i, oldpoints[i], i);
  187.   }
  188. }
  189.  
  190.